/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.d2g.realestate.facade;

import br.com.d2g.realestate.persistence.Announce;
import br.com.d2g.realestate.persistence.Offer;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author d2g
 */
@Stateless
public class OfferFacade extends AbstractFacade<Offer> {

    @PersistenceContext(unitName = "WebApplication2PU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public OfferFacade() {
        super(Offer.class);
    }

    public List<Offer> searchOfferByClient(String username, int[] range) {
        TypedQuery offerByClientQuery = this.em.createNamedQuery("Offer.findByClient", Offer.class);
        offerByClientQuery.setParameter("username", username);
        offerByClientQuery.setMaxResults(range[1] - range[0]);
        offerByClientQuery.setFirstResult(range[0]);
        return offerByClientQuery.getResultList();
    }

    public int countSearchOfferByClient(String username) {
        TypedQuery offerByClientQuery = this.em.createNamedQuery("Offer.findByClient", Offer.class);
        offerByClientQuery.setParameter("username", username);
        return offerByClientQuery.getResultList().size();
    }
    
    public List<Offer> searchOfferByPropertyOwner(String username, int[] range) {
        TypedQuery offerByPropertyOwnerQuery = this.em.createNamedQuery("Offer.findByPropertyOwner", Offer.class);
        offerByPropertyOwnerQuery.setParameter("username", username);
        offerByPropertyOwnerQuery.setMaxResults(range[1] - range[0]);
        offerByPropertyOwnerQuery.setFirstResult(range[0]);
        return offerByPropertyOwnerQuery.getResultList();
    }

    public int countSearchOfferByPropertyOwner(String username) {
        TypedQuery offerByPropertyOwnerQuery = this.em.createNamedQuery("Offer.findByPropertyOwner", Offer.class);
        offerByPropertyOwnerQuery.setParameter("username", username);
        return offerByPropertyOwnerQuery.getResultList().size();
    }
}
